package br.com.edumobi.fragments;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;

import br.com.edumobi.P;
import br.com.edumobi.R;
import br.com.edumobi.utils.Mask;

public class CadastroFragment extends GenericoFragment {

    private TextView textViewNomeTelaCadastroFragment;
    private TextView textViewOrientacaoSenhaFragment;
    private EditText editTextNomeCadastroFragment;
    private EditText editTextEmailCadastroFragment;
    private EditText editTextCpfCadastroFragment;
    private Button buttonCadastrarCadastroFragment;
    private ProgressBar progressBarCadastroFragment;

    private AcoesCadastrarFragment activity;
    private CadastroTask task;

    private String nome;
    private String email;
    private String cpf;

    View.OnClickListener clickButtonCadastrarCadastroFragment = new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            nome = editTextNomeCadastroFragment.getText().toString();
            email = editTextEmailCadastroFragment.getText().toString();
            cpf = editTextCpfCadastroFragment.getText().toString();

            if (nome.trim().equals(P.STRING_EMPITY)) {
                activity.falarEExibirTexto(R.string.obrigatoriedade_nome);
            } else if(email.trim().equals(P.STRING_EMPITY)) {
                activity.falarEExibirTexto(R.string.obrigatoriedade_email);
            } else if (Mask.unmask(cpf).trim().equals(P.STRING_EMPITY)) {
                activity.falarEExibirTexto(R.string.obrigatoriedade_cpf);
            } else {
                if (activity.conectado()) {
                    if (task == null || task.getStatus().equals(AsyncTask.Status.FINISHED)){
                        task = new CadastroTask();
                        task.execute();
                    }
                } else {
                    activity.falarEExibirTexto(R.string.excecao_sem_conexao_internet);
                }
            }
        }
    };

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        activity = (AcoesCadastrarFragment) getActivity();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_cadastro, container, false);

        textViewNomeTelaCadastroFragment = (TextView) view.findViewById(R.id.textViewNomeTelaLoginFragment);
        textViewOrientacaoSenhaFragment = (TextView) view.findViewById(R.id.textViewOrientacaoSenhaFragment);
        editTextNomeCadastroFragment = (EditText) view.findViewById(R.id.editTextNomeCadastroFragment);
        editTextEmailCadastroFragment = (EditText) view.findViewById(R.id.editTextEmailCadastroFragment);
        editTextCpfCadastroFragment = (EditText) view.findViewById(R.id.editTextCpfCadastroFragment);
        buttonCadastrarCadastroFragment = (Button) view.findViewById(R.id.buttonCadastrarCadastroFragment);
        progressBarCadastroFragment = (ProgressBar) view.findViewById(R.id.progressBarCadastroFragment);

        editTextCpfCadastroFragment.addTextChangedListener(Mask.insert("###.###.###-##", editTextCpfCadastroFragment));

        buttonCadastrarCadastroFragment.setOnClickListener(clickButtonCadastrarCadastroFragment);

        if (task != null && task.getStatus().equals(AsyncTask.Status.RUNNING)) {
            esconderComponentes();
            progressBarCadastroFragment.setVisibility(View.VISIBLE);
        }

        return view;
    }

    @Override
    public void exibirComponentes() {
        textViewNomeTelaCadastroFragment.setVisibility(View.VISIBLE);
        textViewOrientacaoSenhaFragment.setVisibility(View.VISIBLE);
        editTextNomeCadastroFragment.setVisibility(View.VISIBLE);
        editTextEmailCadastroFragment.setVisibility(View.VISIBLE);
        editTextCpfCadastroFragment.setVisibility(View.VISIBLE);
        buttonCadastrarCadastroFragment.setVisibility(View.VISIBLE);
        progressBarCadastroFragment.setVisibility(View.VISIBLE);
    }

    @Override
    public void esconderComponentes() {
        textViewNomeTelaCadastroFragment.setVisibility(View.GONE);
        textViewOrientacaoSenhaFragment.setVisibility(View.GONE);
        editTextNomeCadastroFragment.setVisibility(View.GONE);
        editTextEmailCadastroFragment.setVisibility(View.GONE);
        editTextCpfCadastroFragment.setVisibility(View.GONE);
        buttonCadastrarCadastroFragment.setVisibility(View.GONE);
        progressBarCadastroFragment.setVisibility(View.GONE);
    }

    private class CadastroTask extends AsyncTask<Void, Void, Boolean> {

        @Override
        protected void onPreExecute() {
            esconderComponentes();
            progressBarCadastroFragment.setVisibility(View.VISIBLE);
            activity.falarTexto(R.string.aguarde);
        }

        @Override
        protected Boolean doInBackground(Void... params) {
            return activity.cadastrarAluno(nome, email, cpf);
        }

        @Override
        protected void onPostExecute(Boolean aBoolean) {
            exibirComponentes();
            progressBarCadastroFragment.setVisibility(View.GONE);

            if (aBoolean) {
                activity.exibirTexto(R.string.cadastro_realizado_com_sucesso);
            } else {
                activity.falarEExibirTexto(R.string.excecao_cadastro_aluno_malsucedido);
            }
        }
    }

    public interface AcoesCadastrarFragment extends AcoesGenericoFragment {
        boolean cadastrarAluno(String nome, String email, String cpf);
    }
}
